import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws Exception {


        Socket socket = new Socket("127.0.0.1",8888);
        new ClientThread(socket).start();
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dos = new DataOutputStream(outputStream);
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入信息:");

        while (true) {

            String info = scanner.nextLine();

            dos.writeUTF(info);
            dos.flush();

        }


    }
}

class ClientThread extends Thread{
    Socket socket;

    public ClientThread(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {

        try {

            while (true){
                InputStream  inputStream = socket.getInputStream();
                DataInputStream dis = new DataInputStream(inputStream);
                String info = dis.readUTF();
                System.out.println("客户端2:"+info);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
